package com.timoon.webcomponent.utility.titlelist.list;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.page.Page1;
import com.timoon.sample.dao.model.Passage;
import com.timoon.sample.utils.SystemConfig;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

public class TitleListPanel extends Panel {

	@SpringBean(name="sample.passage")
	private TitleListService service;
	
	private PageParameters parameters;
	
	private Integer requestPage=1;//请求页码
	
	private Integer rows=2;//分页大小
	
	private Integer passageType=5;//passage类型
	
	private Integer records;
	
	private Class<? extends WebPage> pageTargetPage;//换页目标页，不出意外的话，都是本页
	
	private Class<? extends WebPage> passageTargetPage;//具体显示一片文章页
	
	public TitleListPanel(String id,PageParameters parameters,Integer rows,
			Class<? extends WebPage> pageTargetPage,Class<? extends WebPage> passageTargetPage) {
		super(id);
		// TODO Auto-generated constructor stub


		if(!parameters.get("requestPage").isNull()){
			requestPage=parameters.get("requestPage").toInteger();//获取上次页码
		}
		if(!parameters.get("actionInfoSortId").isNull()){
			this.passageType=parameters.get("actionInfoSortId").toInteger();
		}
		
		this.parameters=parameters;
		
		this.passageType=passageType;
		
		this.pageTargetPage=pageTargetPage;
		
		this.passageTargetPage=passageTargetPage;
		
		this.rows=rows;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderListView();
		
		renderButtons();
	}

	private void renderButtons() {
		// TODO Auto-generated method stub
		
		Form form=new Form<>("wk_form");
		add(form);
		
		AjaxButton firstBtn=new AjaxButton("wk_first") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				parameters.set("requestPage", requestPage);
				
				this.setResponsePage(pageTargetPage,parameters);
			}
		};
		form.add(firstBtn);
		
		AjaxButton lastBtn=new AjaxButton("wk_last") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				parameters.set("requestPage", requestPage-1);
				
				this.setResponsePage(pageTargetPage,parameters);
			}
		};
		form.add(lastBtn);
		
		AjaxButton nextBtn=new AjaxButton("wk_next") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				parameters.set("requestPage", requestPage+1);
				
				this.setResponsePage(pageTargetPage,parameters);
			}
		};
		form.add(nextBtn);
		
		AjaxButton finalBtn=new AjaxButton("wk_final") {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				PageParameters parameters=new PageParameters();
				parameters.set("requestPage", requestPage);
				
				this.setResponsePage(pageTargetPage,parameters);
			}
		};
		form.add(finalBtn);
		
		Integer maxPage=(this.records-1+rows)/rows;
		
		if(maxPage.compareTo(Integer.valueOf(1))==0){
			firstBtn.setEnabled(false);
			lastBtn.setEnabled(false);
			nextBtn.setEnabled(false);
			finalBtn.setEnabled(false);
			return;
		}
		
		if(this.requestPage.compareTo(Integer.valueOf(1))==0){
			firstBtn.setEnabled(false);
			lastBtn.setEnabled(false);
		}else if(requestPage.compareTo(maxPage)==0){
			nextBtn.setEnabled(false);
			finalBtn.setEnabled(false);
		}
	}

	private void renderListView() {
		// TODO Auto-generated method stub
		
		records=this.service.countPassage(passageType);
		
		List<Passage> passages=this.service.pagingPassage(requestPage, rows, records,passageType);
		
		ListView<Passage> listView=new ListView<Passage>("wk_list",passages) {

			@Override
			protected void populateItem(ListItem<Passage> arg0) {
				// TODO Auto-generated method stub
				
				Passage p=arg0.getModelObject();
				
				PageParameters parameters=new PageParameters();
				parameters.set("passage_id", p.getId());
				
				BookmarkablePageLink link=new BookmarkablePageLink<>("wk_link",passageTargetPage,parameters);
				arg0.add(link);
				
				link.add(new Label("wk_label",p.getTitle()));
				
				arg0.add(new Label("wk_date",p.getUpdateDate()));
			}
		};
		add(listView);
	}

}
